package com.qf.java2102.qihaonan.hotel.controller.food;

import com.qf.java2102.qihaonan.hotel.entity.ResultVO;
import com.qf.java2102.qihaonan.hotel.pojo.Food;
import com.qf.java2102.qihaonan.hotel.service.IFoodService;
import com.qf.java2102.qihaonan.hotel.service.impl.FoodServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author qhn
 * @version 1.0
 */
@WebServlet("/food/findById")
public class FoodFindByIdController extends HttpServlet {

    private IFoodService foodService = new FoodServiceImpl();

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String foodIdStr = request.getParameter("foodId");
        Long foodId = Long.valueOf(foodIdStr == null ? "0" : foodIdStr);
        ResultVO<Food> resultVO = foodService.findById(foodId);
        if(resultVO.getSuccess()) {
            request.setAttribute("food", resultVO.getData());
            request.getRequestDispatcher("/front/detail/caixiangxi.jsp").forward(request, response);
            return;
        }
        response.getWriter().write(resultVO.getMessage());

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
